/*
 * RiskProbabilityBean.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.managedbean;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.itrustsolution.rms.entities.RskRiskProbability;
import com.itrustsolution.rms.util.HibernateUtil;
import com.itrustsolution.rms.util.JSFUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;

/**
 *
 * @author Administrator
 */
public class RiskProbabilityBean {

    // Field
    private HtmlInputText txtId;
    private HtmlInputText txtName;
    private HtmlInputText txtDescription;
    private HtmlCommandButton btnAdd;
    private HtmlCommandButton btnEdit;
    private HtmlCommandButton btnRemove;
    private HtmlCommandButton btnSave;
    private HtmlCommandButton btnCancel;
    // List of all risk severity data
    private List<RskRiskProbability> dataTables = new ArrayList<RskRiskProbability>();

    /** Creates a new instance of RiskProbabilityBean */
    public RiskProbabilityBean() {
    }

    /**
     * Get the data tables
     * @return the dataTables
     */
    public List<RskRiskProbability> getDataTables() {
        dataTables = (ArrayList<RskRiskProbability>) HibernateUtil.createQuery("FROM RskRiskProbability WHERE riskProbIsactive = 1");
        return dataTables;
    }

    /**
     * When row table selected: 
     * 1. Display the form
     * 2. Show edit and remove button
     * @return nothing
     */
    public void rowSelectionListener(RowSelectorEvent e) {
        if (!getBtnSave().isRendered()) {
            // Display selected row
            RskRiskProbability obj = (RskRiskProbability) getDataTables().get(e.getRow());
            getTxtId().setValue(String.valueOf(obj.getRiskProbId()));
            getTxtId().setSubmittedValue(String.valueOf(obj.getRiskProbId()));
            getTxtName().setValue(obj.getRiskProbName());
            getTxtName().setSubmittedValue(obj.getRiskProbName());
            getTxtDescription().setValue(obj.getRiskProbDesc());
            getTxtDescription().setSubmittedValue(obj.getRiskProbDesc());

            // Show edit and remove button only when row selected
            getBtnEdit().setRendered(true);
            getBtnRemove().setRendered(true);
        }
    }

    /**
     * When add clicked: 
     * 1. Reset the form
     * 2. Enable the form
     * 3. Show only save and cancel button
     * @return nothing
     */
    public String add_action() {
        getTxtId().setValue(null);
        getTxtId().setSubmittedValue(null);
        getTxtName().setValue("");
        getTxtName().setSubmittedValue("");
        getTxtName().setDisabled(false);
        getTxtDescription().setValue("");
        getTxtDescription().setSubmittedValue("");
        getTxtDescription().setDisabled(false);

        getBtnAdd().setRendered(false);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(true);
        getBtnCancel().setRendered(true);
        return "";
    }

    /**
     * When edit clicked: 
     * 1. Enable the form
     * 2. Show only save and cancel button
     * @return nothing
     */
    public String edit_action() {
        getTxtName().setDisabled(false);
        getTxtDescription().setDisabled(false);

        getBtnAdd().setRendered(false);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(true);
        getBtnCancel().setRendered(true);
        return "";
    }

    /**
     * When remove clicked: 
     * 1. Delete the selected row
     * @return nothing
     */
    public String remove_action() {
        RskRiskProbability obj;
        String result = "";
        if (getTxtId().getValue() != null) {
            obj = (RskRiskProbability) HibernateUtil.getEntityById(RskRiskProbability.class, Integer.parseInt((String) getTxtId().getValue()));
            obj.setRiskProbIsactive(false);
            HibernateUtil.saveOrUpdate(obj);

            result = txtName.getValue() + " deleted";
            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, result);
            
            cancel_action();
        }
        return "";
    }

        /**
         * When save clicked: 
         * 1. Save the data
         * 2. Reset the form
         * @return nothing
         */
    

    public String save_action() {
        RskRiskProbability obj;
        String result = "";
        if (getTxtId().getValue() == null) { // Add
            obj = new RskRiskProbability();
        } else { // Edit
            //obj = (RskRiskProbability) session.get(RskRiskProbability.class, Integer.parseInt((String) getTxtId().getValue()));
            obj = (RskRiskProbability) HibernateUtil.getEntityById(RskRiskProbability.class, Integer.parseInt((String) getTxtId().getValue()));
        }

        obj.setRiskProbName((String) txtName.getValue());
        obj.setRiskProbDesc((String) txtDescription.getValue());
        obj.setRiskProbIsactive(true);
        HibernateUtil.saveOrUpdate(obj);

        result = txtName.getValue() + " created";
        if (getTxtId().getValue() != null) {
            result = txtName.getValue() + " edited";
        }
        JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, result);
        cancel_action();

        return "";
    }

    /**
     * When cancel clicked: 
     * 1. Reset the form
     * 2. Disable the form
     * 3. Show only add and cancel button
     * @return nothing
     */
    public String cancel_action() {
        getTxtName().setValue("");
        getTxtName().setSubmittedValue("");
        getTxtName().setDisabled(true);
        getTxtDescription().setValue("");
        getTxtDescription().setSubmittedValue("");
        getTxtDescription().setDisabled(true);

        getBtnAdd().setRendered(true);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(false);
        getBtnCancel().setRendered(false);
        return "";
    }

    /**
     * @return the btnAdd
     */
    public HtmlCommandButton getBtnAdd() {
        return btnAdd;
    }

    /**
     * @param btnAdd the btnAdd to set
     */
    public void setBtnAdd(HtmlCommandButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    /**
     * @return the txtName
     */
    public HtmlInputText getTxtName() {
        return txtName;
    }

    /**
     * @param txtName the txtName to set
     */
    public void setTxtName(HtmlInputText txtName) {
        this.txtName = txtName;
    }

    /**
     * @return the txtDescription
     */
    public HtmlInputText getTxtDescription() {
        return txtDescription;
    }

    /**
     * @param txtDescription the txtDescription to set
     */
    public void setTxtDescription(HtmlInputText txtDescription) {
        this.txtDescription = txtDescription;
    }

    /**
     * @return the btnEdit
     */
    public HtmlCommandButton getBtnEdit() {
        return btnEdit;
    }

    /**
     * @param btnEdit the btnEdit to set
     */
    public void setBtnEdit(HtmlCommandButton btnEdit) {
        this.btnEdit = btnEdit;
    }

    /**
     * @return the btnRemove
     */
    public HtmlCommandButton getBtnRemove() {
        return btnRemove;
    }

    /**
     * @param btnRemove the btnRemove to set
     */
    public void setBtnRemove(HtmlCommandButton btnRemove) {
        this.btnRemove = btnRemove;
    }

    /**
     * @return the btnSave
     */
    public HtmlCommandButton getBtnSave() {
        return btnSave;
    }

    /**
     * @param btnSave the btnSave to set
     */
    public void setBtnSave(HtmlCommandButton btnSave) {
        this.btnSave = btnSave;
    }

    /**
     * @return the btnCancel
     */
    public HtmlCommandButton getBtnCancel() {
        return btnCancel;
    }

    /**
     * @param btnCancel the btnCancel to set
     */
    public void setBtnCancel(HtmlCommandButton btnCancel) {
        this.btnCancel = btnCancel;
    }

    /**
     * @return the txtId
     */
    public HtmlInputText getTxtId() {
        return txtId;
    }

    /**
     * @param txtId the txtId to set
     */
    public void setTxtId(HtmlInputText txtId) {
        this.txtId = txtId;
    }

    /**
     * @param dataTables the dataTables to set
     */
    public void setDataTables(List<RskRiskProbability> dataTables) {
        this.dataTables = dataTables;
    }
}
